﻿Public Class FormProduct

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim frm As New FormProductEditor()
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            LoadData()
        End If
    End Sub

    Private Sub FormProduct_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        UI.FormatDataGrid(dgv)
        LoadData()
    End Sub
    Private Sub LoadData()
        Cmd.Parameters("@s") = txtSearch.Text
        Dim dt As DataTable = Cmd.ExecuteDataTable("SELECT Product.ProductCode, Product.ProductName, Type.TypeName,Category.CategoryName,Product.Price, Product.MinimumPrice, Product.Active FROM (Type INNER JOIN Category ON Type.TypeId = Category.TypeId) INNER JOIN Product ON Category.CategoryId = Product.CategoryId WHERE UPPER(ProductName) LIKE UPPER('%'+@s+'%')")
        Me.dgv.DataSource = dt
    End Sub

    Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
        LoadData()
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click

    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If Me.dgv.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        If MessageBox.Show("Are you sure want to delete ?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) <> Windows.Forms.DialogResult.Yes Then
            Exit Sub
        End If
        Dim row = Me.dgv.SelectedRows(0)
        Dim id = row.Cells("ProductId").Value


        Cmd.Parameters("@ProductId") = id
        Cmd.ExecuteNonQuery("UPDATE Product SET Active=0 WHERE ProductId=@ProductId")

        LoadData()
    End Sub
End Class